package cn.glinks.liuhe.adpter;

import android.content.Context;
import android.support.v7.widget.AppCompatTextView;
import android.support.v7.widget.RecyclerView;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.style.ForegroundColorSpan;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import butterknife.BindView;
import butterknife.ButterKnife;
import cn.glinks.liuhe.R;
import cn.glinks.liuhe.api.jsonbean.SearchResultInfo;


public class SearchResultAdapter extends RecyclerView.Adapter<SearchResultAdapter.ViewHolder> implements View.OnClickListener {

    private Context mContext;
    private LayoutInflater mLayoutInflater;
    private List<SearchResultInfo> mFeedList;
    private String HighlightKey = "";

    public SearchResultAdapter(List<SearchResultInfo> feedList) {
        mFeedList = feedList;
    }

    //创建新View，被LayoutManager所调用
    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        mContext = parent.getContext();
        mLayoutInflater = LayoutInflater.from(mContext);
        View view = mLayoutInflater.inflate(R.layout.item_search_result, parent, false);
        ViewHolder vh = new ViewHolder(view);
        //将创建的View注册点击事件
        view.setOnClickListener(this);
        return vh;
    }

    //将数据与界面进行绑定的操作
    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {

        final SearchResultInfo info = mFeedList.get(position);
        //将数据保存在itemView的Tag中，以便点击时进行获取
        holder.itemView.setTag(info);
        // 获取原始字符串
        String str = info.getName() + "(" + info.getEmid() + ")";
        // 构建高亮字符串
        if (HighlightKey.isEmpty()) {
            holder.mNameTextView.setText(str);
        } else {
            SpannableString s = new SpannableString(str);
            Pattern p = Pattern.compile(HighlightKey);
            Matcher m = p.matcher(s);
            if (m.find()) {
                int start = m.start();
                int end = m.end();
                s.setSpan(new ForegroundColorSpan(0xFF6989FF), start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
            }
            holder.mNameTextView.setText(s);
        }

    }

    //获取数据的数量
    @Override
    public int getItemCount() {
        return (mFeedList != null ? mFeedList.size() : 0);
    }

    //item的点击动作
    @Override
    public void onClick(View v) {
        if (mOnItemClickListener != null) {
            //注意这里使用getTag方法获取数据
            mOnItemClickListener.onItemClick(v,(SearchResultInfo)v.getTag());
        }
    }

    //自定义的ViewHolder，持有每个Item的的所有界面元素
    public static class ViewHolder extends RecyclerView.ViewHolder {

        @BindView(R.id.tv_name)
        AppCompatTextView mNameTextView;

        public ViewHolder(View view) {
            super(view);
            ButterKnife.bind(this, view);
        }
    }

    // 设置高亮字段
    public void setHighlightKey(String key) {
        HighlightKey = key;
    }

    // 清空数据
    public void clearItems() {
        if (mFeedList != null) {
            mFeedList.clear();
            notifyDataSetChanged();
        }
    }

    // 添加数据
    public void addItem(SearchResultInfo info) {
        if (mFeedList == null) {
            mFeedList = new ArrayList<>();
        }
        mFeedList.add(info);
        notifyDataSetChanged();
    }

    public static interface OnItemClickListener {
        void onItemClick(View view , SearchResultInfo info);
    }

    private OnItemClickListener mOnItemClickListener = null;
    public void setOnItemClickListener(OnItemClickListener listener) {
        this.mOnItemClickListener = listener;
    }

}
